//
//  ServiceHolder+Collection.swift
//  CooperSwift
//
//  Created by 计海峰 on 2022/2/23.
//

import Foundation

// MARK: - 转接 - collection
public extension ServiceHolder {
    
    /// 通过key搜索tablle的indexpath
    func byKeySearchIndexPath(_ parent: CollectionViewModelProtocol, key: String) -> IndexPath? {
        for (sectionIdx, section) in parent.sections.enumerated() {
            for (index, model) in section.items.enumerated() {
                if let info = model as? BindKeyProtocol {
                    if info.bindKey() == key {
                        return IndexPath(row: index, section: sectionIdx)
                    }
                }
            }
        }
        return nil
    }
    
    /// reload cell
    @objc func serviceReloadCollectionItem(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? CollectionViewModelProtocol else { return }
        guard let cv = parentTarget.currentCV() else { return }
        guard let indexPath = byKeySearchIndexPath(parentTarget, key: key) else { return }
        DispatchQueue.main.async {
            cv.reloadItemsAtIndexPaths([indexPath], animationStyle: .none)
        }
    }
    
    /// reload section
    @objc func serviceReloadCollectionSection(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? CollectionViewModelProtocol else { return }
        guard let cv = parentTarget.currentCV() else { return }
        guard let indexPath = byKeySearchIndexPath(parentTarget, key: key) else { return }
        DispatchQueue.main.async {
            cv.reloadSections([indexPath.section], animationStyle: .none)
        }
    }
    
    /// reload table
    @objc func serviceReloadCollection (_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? CollectionViewModelProtocol else { return }
        guard let cv = parentTarget.currentCV() else { return }
        DispatchQueue.main.async {
            cv.reloadData()
        }
    }
    
    /// remove cell
    @objc func serviceRemoveCollectionItem(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? CollectionViewModelProtocol else { return }
        guard let indexPath = byKeySearchIndexPath(parentTarget, key: key) else { return }
        guard var section = parentTarget.sections[safe: indexPath.section] else { return }
        section.items.remove(at: indexPath.row)
        parentTarget.sections[indexPath.section] = section
    }
    
    /// insert cell
    @objc func serviceInsertCollectionItem(_ info: Any, parent: Any) {
        guard let parentTarget = parent as? CollectionViewModelProtocol else { return }
        guard let target = parent as? (CollectionCellConfigProtocol, IndexPath) else { return }
        guard var section = parentTarget.sections[safe: target.1.section] else { return }
        section.items.insert(target.0, at: target.1.row)
        parentTarget.sections[target.1.section] = section
    }
    
    @objc func serviceInsertCollectionItemFirst(_ info: Any, parent: Any) {
        guard let parentTarget = parent as? CollectionViewModelProtocol else { return }
        guard let target = parent as? (CollectionCellConfigProtocol, (Int, Int)) else { return }
        guard var section = parentTarget.sections[safe: target.1.0] else { return }
        section.items.insert(target.0, at: target.1.1)
        parentTarget.sections[target.1.0] = section
    }
    
    @objc func serviceInsertCollectionItemIndex(_ info: Any, parent: Any) {
        guard let parentTarget = parent as? CollectionViewModelProtocol else { return }
        guard let target = info as? (CollectionCellConfigProtocol, (Int, Int)) else { return }
        guard var section = parentTarget.sections[safe: target.1.0] else { return }
        section.items.insert(target.0, at: target.1.0)
        parentTarget.sections[target.1.0] = section
    }
    
    @objc func serviceInsertCollectionItemLast(_ info: Any, parent: Any) {
        guard let parentTarget = parent as? CollectionViewModelProtocol else { return }
        guard let target = info as? (CollectionCellConfigProtocol, (Int, Int)) else { return }
        guard var section = parentTarget.sections[safe: target.1.0] else { return }
        section.items.insert(target.0, at: section.items.count - target.1.1)
        parentTarget.sections[target.1.0] = section
    }
    
    /// toTableCellTop
    @objc func toCollectionItemTop(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? CollectionViewModelProtocol else { return }
        guard let cv = parentTarget.currentCV() else { return }
        guard let indexPath = byKeySearchIndexPath(parentTarget, key: key) else { return }
        DispatchQueue.main.async {
            cv.scrollToItem(at: indexPath, at: .top, animated: false)
        }
    }

    /// toTableCellBottom
    @objc func toCollectionItemBottom(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? CollectionViewModelProtocol else { return }
        guard let cv = parentTarget.currentCV() else { return }
        guard let indexPath = byKeySearchIndexPath(parentTarget, key: key) else { return }
        DispatchQueue.main.async {
            cv.scrollToItem(at: indexPath, at: .bottom, animated: false)
        }
    }

}
